/*
 * @Author: lmh
 * @Date: 2021-08-11 10:38:16
 * @LastEditTime: 2021-08-24 16:57:05
 * @LastEditors: lmh
 * @Description: 路由
 */
import { createRouter, createWebHashHistory } from 'vue-router';

export const routes = [
  {
    path: '/',
    name: '伤口图片',
    icon: 'UserOutlined',
    component: () => import('@/components/Layout'),
    redirect: '/index',
    children: [
      {
        path: '/index',
        name: '主页',
        component: () => import('@/views/home/index'),
      },
      {
        path: '/list',
        name: '导出记录',
        hiden: true,
        component: () => import('@/views/home/list'),
      },
      {
        path: '/detail',
        name: '患者详情',
        hiden: true,
        component: () => import('@/views/home/detail'),
      },
    ],
  },
  {
    path: '/user',
    name: '用户管理',
    icon: 'FormOutlined',
    component: () => import('@/components/Layout'),
    children: [
      {
        path: '/user/index',
        name: '用户管理员',
        component: () => import('@/views/user/index'),
      },
      {
        path: '/user/add',
        name: '添加用户',
        hiden: true,
        component: () => import('@/views/user/add'),
      },
      {
        path: '/user/edit',
        name: '编辑用户',
        hiden: true,
        component: () => import('@/views/user/edit'),
      },
      {
        path: '/editPassword',
        name: '修改密码',
        hiden: true, // 不在菜单中显示
        component: () => import('../views/user/editPassword'),
      },
      {
        path: '/user/departManagement',
        name: '部门管理',
        component: () => import('@/views/user/departManagement'),
      },
    ],
  },
  {
    path: '/workforceManagement',
    name: '排班管理',
    icon: 'FormOutlined',
    component: () => import('@/components/Layout'),
    children: [
      {
        path: '/workforceManagement/index',
        name: '排班管理',
        component: () => import('@/views/workforceManagement/index'),
      },
    ],
  },
  {
    path: '/login',
    name: 'login',
    hiden: true, // 不在菜单中显示
    component: () => import('../views/login'),
  },
];

const router = createRouter({
  history: createWebHashHistory(),
  routes,
});

router.beforeEach((to, from, next) => {
  if (to.matched.some(record => record.meta.requiresAuth)) {
    //对matched不了解的建议看官方api文档,或我7.1节的说明
    // //数组some方法,如果meta.requiresAuth为ture,则返回true.此时,说明进入该路由前需要判断用户是否已经登录 
    //   if (!auth.loggedIn()) {   //如果没登录,则跳转到登录页
    //     next({
    //       path: '/login',
    //       query: { redirect: to.fullPath }  //官方例子的这个小细节很好,通过query将要跳转的路由路径保存下来,待完成登录后,就可以直接获取该路径,直接跳转到登录前要去的路由
    //     })
    //   } else {
    //     next()
    //   }
  } else {
    next() // 确保一定要调用 next()
  }
})

export default router;
